package cn.com.utt.unms.mapper;

import java.util.Collection;
import java.util.List;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

@Mapper
public interface GroupNetworkMapper {
	
	@Insert("insert into `group_network`(group_id,network_id) values(#{groupId},#{networkId})")
	void save(@Param("groupId")long groupId,@Param("networkId")long networkId);
	
	@Select({"<script>",
	    "insert into group_network(group_id,network_id) values", 
	      "<foreach item='item' index='index' collection='groupIds' separator=','>",
	        "(#{item},#{networkId})",
	      "</foreach>",
	    "</script>"})
	void batchSave(@Param("groupIds")Collection<Long> groupIds,@Param("networkId")long networkId);
	
	@Delete({"<script>",
	    "delete ", 
	    "FROM `group_network`",
	    "WHERE ", 
	    "network_id IN", 
	      "<foreach item='item' index='index' collection='networkIds'",
	        "open='(' separator=',' close=')'>",
	        "#{item}",
	      "</foreach>",
	"</script>"})
	void deleteByNetworkIds(@Param("networkIds")List<Long> networkIds);
	
	@Select("SELECT network_id FROM `group_network` WHERE group_id=#{groupId}")
	List<Long> queryByGroupId(@Param("groupId")long groupId);
	
	@Delete("delete FROM `group_network` WHERE network_id=#{networkId}")
	void deleteByNetworkId(@Param("networkId")long networkId);
	
}
